<template>
    <div class="createPost-container">
        <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
            <div class="createPost-main-container">
                <el-form-item prop="store_id" style="margin-bottom: 40px;" label-width="90px" :label="alias.store">
                    <el-select v-model="postForm.store_id">
                        <el-option :value="0" :label="'平台'">平台</el-option>
                        <el-option v-for="(store, index) in store_list" :key="index" :label="store.name" :value="store.id">{{ store.name }}</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="name" style="margin-bottom: 40px;" label-width="90px" label="姓名:">
                    <el-input v-model="postForm.name" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item prop="mobile" style="margin-bottom: 40px;" label-width="90px" label="手机号:">
                    <el-input v-model="postForm.mobile" placeholder="请输入手机号" />
                </el-form-item>
                <el-button v-loading="loading" style="margin-left: 10px;" type="primary" @click="submitForm">
                    保存
                </el-button>
            </div>
        </el-form>
    </div>
</template>

<script>
import store from '@/store/index'
import { fetch, update, create } from '@/clerk/api/clerk'
import { queryList as queryStoreList } from '@/shop/api/store'

const defaultForm = {
    store_id: '',
    name: '',
    mobile: ''
}

export default {
    name: 'ClerkDetail',
    props: {
        isEdit: {
            type: Boolean,
            default: false
        }
    },
    data() {
        const validateRequire = (rule, value, callback) => {
            if (value === '' || value === null || value === undefined) {
                this.$message({
                    message: rule.message,
                    type: 'error'
                })
                callback(new Error())
            } else {
                callback()
            }
        }
        const validateMobile = (rule, value, callback) => {
            var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/
            if (!myreg.test(value)) {
                this.$message({
                    message: rule.message,
                    type: 'error'
                })
                callback(new Error())
            } else {
                callback()
            }
        }
        return {
            alias: {
                store: store.state.conf.alias.store
            },
            id: 0,
            postForm: Object.assign({}, defaultForm),
            typeList: [],
            loading: false,
            userListOptions: [],
            rules: {
                store_id: [{ validator: validateRequire, message: store.state.conf.alias.store + '为必填字段', trigger: 'blur' }],
                name: [{ validator: validateRequire, message: '姓名为必填字段', trigger: 'blur' }],
                mobile: [{ validator: validateMobile, message: '请输入有效手机号', trigger: 'blur' }]
            },
            tempRoute: {},
            dialogFormGps: false,
            store_list: []
        }
    },
    created() {
        queryStoreList().then(res => {
            this.store_list = res.data.items
        })
        if (this.isEdit === true) {
            this.id = this.$route.query.id
            this.fetchData()
        }
    },
    methods: {
        fetchData() {
            fetch({ id: this.id }).then(response => {
                this.postForm.name = response.data.clerk.name
                this.postForm.mobile = response.data.clerk.mobile
                this.postForm.store_id = response.data.clerk.store_id
                // this.postForm.img_display = response.data.store.img_display
                // this.postForm.prov = response.data.store.prov
                // this.postForm.city = response.data.store.city
                // this.postForm.district = response.data.store.district
                // this.postForm.address = response.data.store.address
                // this.postForm.lng = response.data.store.lng
                // this.postForm.lat = response.data.store.lat
                // if (response.data.store.img_display !== null && response.data.store.img_display !== '' && response.data.store.img_display !== undefined) {
                //   this.fileList.push({
                //     name: response.data.store.img_display,
                //     url: asset(response.data.store.img_display)
                //   })
                // }
                // set page title
            }).catch(err => {
                console.log(err)
            })
        },
        submitForm() {
            this.$refs.postForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    if (this.isEdit === true) {
                        const data = this.postForm
                        data.id = this.id
                        update(data).then(response => {
                            this.loading = false
                            if (response.errorCode === 200) {
                                this.$notify({
                                    title: '成功',
                                    message: '修改成功',
                                    type: 'success',
                                    duration: 2000
                                })
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: response.errorMsg,
                                    type: 'error',
                                    duration: 2000
                                })
                            }
                        }).catch(err => {
                            console.log(err)
                            this.loading = false
                        })
                    } else {
                        const data = this.postForm
                        create(data).then(response => {
                            this.loading = false
                            if (response.errorCode == 200) {
                                this.$notify({
                                    title: '成功',
                                    message: '添加成功',
                                    type: 'success',
                                    duration: 2000
                                })
                                // this.postForm.status = 'published'
                                this.$router.push('/crm/clerk')
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: response.errorMsg,
                                    type: 'error',
                                    duration: 2000
                                })
                            }
                        }).catch(err => {
                            console.log(err)
                            this.loading = false
                        })
                    }
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        draftForm() {
            if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
                this.$message({
                    message: '请填写必要的标题和内容',
                    type: 'warning'
                })
                return
            }
            this.$message({
                message: '保存成功',
                type: 'success',
                showClose: true,
                duration: 1000
            })
            this.postForm.status = 'draft'
        }
    }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
    position: relative;

    .createPost-main-container {
        padding: 40px 45px 20px 50px;

        .postInfo-container {
            position: relative;
            @include clearfix;
            margin-bottom: 10px;

            .postInfo-container-item {
                float: left;
            }
        }
    }

    .word-counter {
        width: 40px;
        position: absolute;
        right: 10px;
        top: 0px;
    }
}

.article-textarea ::v-deep {
    textarea {
        padding-right: 40px;
        resize: none;
        border: none;
        border-radius: 0px;
        border-bottom: 1px solid #bfcbd9;
    }
}
</style>
